<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Define decimal format -->
  <xsl:decimal-format name="pt-br-Decimal" grouping-separator="." digit="#" zero-digit="0" decimal-separator="," />

  <xsl:variable name="visibleBacklogItemHours" select="/ProjectDataSet/Project/BacklogItem/BacklogItemHours[BaseLine = /ProjectDataSet/Project/Baseline]"/>

  <xsl:template name="MSProjectHeader">

    <SaveVersion>12</SaveVersion>
    <Name><xsl:value-of select="/ProjectDataSet/Project/ProjectName"/>.xml</Name>    
    <ScheduleFromStart>1</ScheduleFromStart>
    <StartDate><xsl:value-of select="substring(/ProjectDataSet/Project/EstimatedStartDate, 1, 19)"/></StartDate>    
    <FYStartDate>1</FYStartDate>
    <CriticalSlackLimit>0</CriticalSlackLimit>
    <CurrencyDigits>2</CurrencyDigits>
    <CurrencySymbol>R$</CurrencySymbol>
    <CurrencyCode>BRL</CurrencyCode>
    <CurrencySymbolPosition>0</CurrencySymbolPosition>
    <CalendarUID>1</CalendarUID>
    <DefaultStartTime>09:00:00</DefaultStartTime>
    <DefaultFinishTime>18:00:00</DefaultFinishTime>
    <MinutesPerDay>480</MinutesPerDay>
    <MinutesPerWeek>2400</MinutesPerWeek>
    <DaysPerMonth>20</DaysPerMonth>
    <DefaultTaskType>0</DefaultTaskType>
    <DefaultFixedCostAccrual>3</DefaultFixedCostAccrual>
    <DefaultStandardRate>0</DefaultStandardRate>
    <DefaultOvertimeRate>0</DefaultOvertimeRate>
    <DurationFormat>7</DurationFormat>
    <WorkFormat>2</WorkFormat>
    <EditableActualCosts>0</EditableActualCosts>
    <HonorConstraints>0</HonorConstraints>
    <InsertedProjectsLikeSummary>1</InsertedProjectsLikeSummary>
    <MultipleCriticalPaths>0</MultipleCriticalPaths>
    <NewTasksEffortDriven>1</NewTasksEffortDriven>
    <NewTasksEstimated>1</NewTasksEstimated>
    <SplitsInProgressTasks>1</SplitsInProgressTasks>
    <SpreadActualCost>0</SpreadActualCost>
    <SpreadPercentComplete>0</SpreadPercentComplete>
    <TaskUpdatesResource>1</TaskUpdatesResource>
    <FiscalYearStart>0</FiscalYearStart>
    <WeekStartDay>0</WeekStartDay>
    <MoveCompletedEndsBack>0</MoveCompletedEndsBack>
    <MoveRemainingStartsBack>0</MoveRemainingStartsBack>
    <MoveRemainingStartsForward>0</MoveRemainingStartsForward>
    <MoveCompletedEndsForward>0</MoveCompletedEndsForward>
    <BaselineForEarnedValue>0</BaselineForEarnedValue>
    <AutoAddNewResourcesAndTasks>1</AutoAddNewResourcesAndTasks>
    <CurrentDate>
      <xsl:value-of select="/ProjectDataSet/Project/BacklogEmmitionDateString"/>
    </CurrentDate>
    <MicrosoftProjectServerURL>1</MicrosoftProjectServerURL>
    <Autolink>1</Autolink>
    <NewTaskStartDate>0</NewTaskStartDate>
    <DefaultTaskEVMethod>0</DefaultTaskEVMethod>
    <ProjectExternallyEdited>0</ProjectExternallyEdited>
    <ExtendedCreationDate>1984-01-01T00:00:00</ExtendedCreationDate>
    <ActualsInSync>1</ActualsInSync>
    <RemoveFileProperties>0</RemoveFileProperties>
    <AdminProject>0</AdminProject>
    <OutlineCodes/>
    <WBSMasks/>
    <ExtendedAttributes/>


  </xsl:template>

  <xsl:template name="MSProjectResources">

    <Resources>
      <xsl:for-each select="/ProjectDataSet/Project/BacklogItemUnit">
        <xsl:sort select="UnitName"/>      
      
        <Resource>
          <UID><xsl:value-of select="position()"/></UID>
          <Name><xsl:value-of select="UnitName"/></Name>
          <Initials><xsl:value-of select="UnitName"/></Initials>
          <Type>1</Type>
          <IsNull>0</IsNull>
          <CanLevel>1</CanLevel>
        </Resource>
      </xsl:for-each>

    </Resources>
  </xsl:template>

  <xsl:template name="MSProjectSprints">
    <xsl:param name="startUId" select="2"/>

    <xsl:for-each select="/ProjectDataSet/Project/Sprint">
      <xsl:sort select="SprintNumber" data-type="number"/>

      <xsl:variable name="sprintUId" select="SprintUId" />
      <xsl:variable name="sprintNumber" select="SprintNumber" />
      <xsl:variable name="sprintItems" select="/ProjectDataSet/Project/BacklogItem[sum(BacklogItemHours[SprintUId = $sprintUId]/Quantity)>0]"/>

      <xsl:variable name="sprintStart">
        <xsl:choose>
          <xsl:when test="SprintNumber = 1">
            <xsl:value-of select="/ProjectDataSet/Project/EstimatedStartDate"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select ="/ProjectDataSet/Project/Sprint[SprintNumber = ($sprintNumber - 1)]/ExpirationDate"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      
      <xsl:variable name="uId" select="$startUId + (($sprintNumber - 1) * 100)"/>
      
      <xsl:call-template name="MSProjectTask">
        <xsl:with-param name="uId" select="$uId"/>
        <xsl:with-param name="name" select="concat('Sprint ', SprintNumber)"/>
        <xsl:with-param name="wbs" select="concat('1.', SprintNumber)"/>
        <xsl:with-param name="level" select="2"/>
        <xsl:with-param name="type" select="1"/>
        <xsl:with-param name="summary" select="1"/>
        <xsl:with-param name="start" select="$sprintStart"/>
        <xsl:with-param name="finish" select="ExpirationDate"/>
        <xsl:with-param name ="milestone" select="1"/>        
      </xsl:call-template>
      
      <xsl:call-template name="MSProjectBacklogItems">
        <xsl:with-param name="startUId" select="$uId + 1"/>
        <xsl:with-param name="sprintNumber" select="$sprintNumber"/>
        <xsl:with-param name="sprintUId" select="$sprintUId"/>
      </xsl:call-template>

    </xsl:for-each>

    
  </xsl:template>

  <xsl:template name="MSProjectBacklogItems">    
    <xsl:param name="sprintUId"/>
    <xsl:param name="sprintNumber"/>
    <xsl:param name="startUId"/>
    
    <xsl:for-each select="/ProjectDataSet/Project/BacklogItem[sum(BacklogItemHours[SprintUId = $sprintUId and (BaseLine = /ProjectDataSet/Project/Baseline or BaseLine = $sprintNumber)]/Quantity)>0]">
      <xsl:sort select="BusinessValue" data-type="number" order="descending" />

      <xsl:variable name="duration" select="sum(BacklogItemHours[SprintUId = $sprintUId and (BaseLine = /ProjectDataSet/Project/Baseline or BaseLine = $sprintNumber)]/Quantity)"/>
      
      <xsl:call-template name="MSProjectTask">
        <xsl:with-param name="uId" select="$startUId + position()"/>
        <xsl:with-param name="name" select="concat(Title, ' [', BacklogItemNumber, ']')"/>
        <xsl:with-param name="wbs" select="concat('1.', $sprintNumber, '.', position())"/>
        <xsl:with-param name="level" select="3"/>
        <xsl:with-param name="duration" select="concat('PT', $duration, 'H0M0S')"/>
        <xsl:with-param name="backlogItemStatus" select ="Status"/>
        <xsl:with-param name="priority" select="BusinessValue"/>
      </xsl:call-template>
      
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="MSProjectAssignments">
    <xsl:param name="startUId" select="2"/>

    <Assignments>
      <xsl:for-each select="/ProjectDataSet/Project/Sprint">
        <xsl:sort select="SprintNumber" data-type="number"/>
        <xsl:variable name="sprintUId" select ="SprintUId"/>
        <xsl:variable name="sprintNumber" select ="SprintNumber"/>

        <xsl:for-each select="/ProjectDataSet/Project/BacklogItem[sum(BacklogItemHours[SprintUId = $sprintUId and (BaseLine = /ProjectDataSet/Project/Baseline or BaseLine = $sprintNumber)]/Quantity)>0]">
          <xsl:sort select="BusinessValue" data-type="number" order="descending"/>

          <xsl:variable name="assigmentUId" select="(($sprintNumber - 1) * 1000) + (position()*8)"/>          
          
          <xsl:variable name="taskUId" select ="$startUId + (($sprintNumber - 1) * 100) + 4 + position()"/>

          <xsl:variable name="totalDuration" select="sum(BacklogItemHours[SprintUId = $sprintUId and (BaseLine = /ProjectDataSet/Project/Baseline or BaseLine = $sprintNumber)]/Quantity)"/>

          <xsl:for-each select ="BacklogItemHours[SprintUId = $sprintUId and (BaseLine = /ProjectDataSet/Project/Baseline or BaseLine = $sprintNumber) and Quantity > 0]">


            <xsl:variable name="fractionDuration" select="Quantity div $totalDuration"/>
            
            <xsl:variable name="unitName" select ="UnitName"/>
            <xsl:variable name="resourceUId">
              <xsl:for-each  select="/ProjectDataSet/Project/BacklogItemUnit">
                <xsl:sort select="UnitName"/>
                <xsl:if test ="UnitName=$unitName">
                  <xsl:value-of select ="position()"/>
                </xsl:if>                
              </xsl:for-each>
            </xsl:variable>
            
            <Assignment>
              <UID>
                <xsl:value-of select ="$assigmentUId + position()"/>
              </UID>
              <TaskUID>
                <xsl:value-of select ="$taskUId"/>
              </TaskUID>
              <ResourceUID>
                <xsl:value-of select="$resourceUId"/>
              </ResourceUID>
              <RegularWork>PT<xsl:value-of select="Quantity"/>H0M0S</RegularWork>
              <RemainingWork>PT<xsl:value-of select="Quantity"/>H0M0S</RemainingWork>              
              <Work>PT<xsl:value-of select="Quantity"/>H0M0S</Work>
              <Units><xsl:value-of select="$fractionDuration"/></Units>
            </Assignment>

          </xsl:for-each>

        </xsl:for-each>


      </xsl:for-each>
    </Assignments>
      
    </xsl:template>
  
  <xsl:template name="MSProjectTask">
    <xsl:param name="uId"/>
    <xsl:param name="name"/>
    <xsl:param name="type" select="0"/>
    <xsl:param name="summary" select="0"/>
    <xsl:param name="wbs"/>
    <xsl:param name="level"/>
    <xsl:param name="duration"/>
    <xsl:param name="durationFormat" select="'5'"/>
    <xsl:param name="start"/>
    <xsl:param name="finish"/>
    <xsl:param name="predecessorUId"/>
    <xsl:param name="backlogItemStatus"/>
    <xsl:param name="milestone" select="0"/>
    <xsl:param name="priority"/>
    <Task>
      <UID><xsl:value-of select="$uId"/></UID>
      <Name><xsl:value-of select="$name"/></Name>
      <Type><xsl:value-of select="$type"/></Type>
      <IsNull>0</IsNull>
      <WBS><xsl:value-of select="$wbs"/></WBS>
      <OutlineNumber><xsl:value-of select="$level"/></OutlineNumber>
      <OutlineLevel><xsl:value-of select="$level"/></OutlineLevel>
      <Duration><xsl:value-of select="$duration"/></Duration>
      <DurationFormat><xsl:value-of select="$durationFormat"/></DurationFormat>     
      <xsl:if test="$start">
        <Start>
          <xsl:value-of select ="substring($start,1,19)"/>
        </Start>
        <ConstraintType>4</ConstraintType>        
        <ConstraintDate>
          <xsl:value-of select ="substring($start,1,19)"/>
        </ConstraintDate>        
      </xsl:if>
      <xsl:if test ="$finish">
        <Finish>
          <xsl:value-of select ="substring($finish,1,19)"/>          
        </Finish>
        <Deadline>
          <xsl:value-of select ="substring($finish,1,19)"/>
        </Deadline>
      </xsl:if>
      <Summary>
        <xsl:value-of select="$summary"/>
      </Summary>
      <xsl:if test="$predecessorUId">
        <PredecessorLink>
          <PredecessorUID>
            <xsl:value-of select ="$predecessorUId"/>
          </PredecessorUID>
          <Type>1</Type>
          <CrossProject>0</CrossProject>
          <LinkLag>0</LinkLag>
          <LagFormat>7</LagFormat>
        </PredecessorLink>
      </xsl:if>
      
        <xsl:choose>
          <xsl:when test ="$backlogItemStatus = 1">
            <PercentComplete>50</PercentComplete>
          </xsl:when>
          <xsl:when test ="$backlogItemStatus = 2">
            <PercentComplete>100</PercentComplete>
          </xsl:when>
        </xsl:choose>

      <Milestone>
        <xsl:value-of select="$milestone"/>
      </Milestone>

      <xsl:if test="$priority and $priority &lt; 1000">
        <Priority>          
          <xsl:value-of select ="$priority"/>
        </Priority>
      </xsl:if>



    </Task>
    
  </xsl:template>
  
  
</xsl:stylesheet>



